package fifis.network.server;

import java.util.StringTokenizer;

/**
 *
 * @author porfiriopartida
 */
public class TestServer extends FifisServer{

    public TestServer(ServerReporter serverReporter, int port){
        this(serverReporter,port,"localhost");
    }
    public TestServer(ServerReporter serverReporter, int port, String serverName){
//        super(serverReporter,port,serverName,100);
        super(serverReporter,port,serverName);
    }
//    public TestServer(ServerReporter serverReporter, int port, String serverName, int maxConnections){
//        super(serverReporter,port,serverName,maxConnections);
//    }
    
    @Override
    public void handle(int id, String msg) {
        msg = msg.trim();
        
        final ClientHandle ch = clients.get(id);
        System.out.println(ch.getNickname() +" says: "+msg);
        String fixed = msg.toLowerCase();
        if(fixed.startsWith("./bc")){
            fixed = "[BC][ "+ch.getNickname()+" ] : "+ msg.substring("./bc".length()).trim();
            broadCast(fixed);
        } else if(fixed.startsWith("./sc")){
            try {
                fixed = msg.substring("./sc".length()).trim();
                StringTokenizer st = new StringTokenizer(fixed);
                if (!st.hasMoreTokens()) {
                    return;
                }
                String user = st.nextToken();
                int uid = Integer.parseInt(user);
                fixed = "[PM][ " + ch.getNickname() + " ] : " + msg.substring(user.length()).trim();
                if(!singleCast(uid, fixed)){
                    ch.send("Couldn't send message to user.");
                }
            } catch (NumberFormatException numberFormatException) {
                ch.send("Invalid user");
            }
        } else if(fixed.contains("hello")){
            ch.send("Server: Hello you. (:");
        }else if(fixed.contains("bye")){
            ch.send("Server: You leave so soon? ):");
        }else
            ch.send("Server: "+msg);
        
        /*
        new Thread(){
            @Override
            public void run(){
                try {
                    sleep(3000);
                } catch (InterruptedException ex) {
                    Logger.getLogger(TestServer.class.getName()).log(Level.SEVERE, null, ex);
                }
                
                ch.finalizeClient();
            }
        }.start();
        */
    }
    public static void main(String args[]){
        ServerReporter reporter = new ServerReporter(){

            @Override
            public void serverReport(String msg) {
                System.err.println("");
                System.err.println("Report...");
                System.err.println(msg);
                System.err.println("");
            }

            @Override
            public void serverReportError(String msg) {
                System.err.println("");
                System.err.println("Error found!");
                System.err.println(msg);
                System.err.println("");
            }
        
        };
        new TestServer(reporter, 1234).start();
    }

    @Override
    public void clientConnected(ClientHandle c) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    public void clientDisconnected(ClientHandle c) {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void beforeStart() {
//        throw new UnsupportedOperationException("Not supported yet.");
    }

    @Override
    protected void afterStart() {
//        throw new UnsupportedOperationException("Not supported yet.");
    }
}
